package com.yycx.module.bus.provider.service.hy.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.RedisUtils;
import com.yycx.module.admin.provider.service.BaseDataScopeService;
import com.yycx.module.admin.provider.service.BaseRoleService;
import com.yycx.module.bus.provider.mapper.hy.HyErpSearchMapper;
import com.yycx.module.bus.provider.service.hy.HyErpSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 海鹰销售单接口实现类
 *
 * @author flyme
 * @date 2020-08-17
 */
@Service
@DS("hy")
public class HyErpSearchServiceImpl extends BaseErpServiceImp implements HyErpSearchService {

    @Autowired
    private HyErpSearchMapper hyErpSearchMapper;

    @Autowired
    private BaseDataScopeService dataScopeService;

    @Autowired
    private BaseRoleService baseRoleService;


    /**
     * 采购单分页列表
     *
     * @param dbName
     * @param params
     * @param isPage
     * @return
     */
    @Override
    public ResultBody procureList(String dbName, Map params, Boolean isPage) {
        initParams(params);
        List<EntityMap> list = hyErpSearchMapper.procureList(dbName, params);
        if (isPage) {
            int total = hyErpSearchMapper.countProcureList(dbName, params);
            EntityMap pageModel = getPageModel(params, list, total);
            return ResultBody.ok(pageModel);
        } else {
            return ResultBody.ok(list);
        }
    }


    /**
     * 采购明细分页列表
     *
     * @param dbName
     * @param params
     * @param isPage
     * @return
     */
    @Override
    public ResultBody procureDetailList(String dbName, Map params, Boolean isPage) {
        initParams(params);
        params.put("isPage", isPage);
        List<String> drugsIds = getDrugsIds(dbName);
        params.put("drugsIds", drugsIds);
        List<String> storeCodes = getStoreCodes(dbName);
        params.put("storeCodes", storeCodes);
        List<String> drugTypes = getDrugType(dbName);
        params.put("drugTypes", drugTypes);
        List<EntityMap> list = hyErpSearchMapper.procureDetailList(dbName, params);
        if (isPage) {
            int total = hyErpSearchMapper.countProcureDetailList(dbName, params);
            EntityMap pageModel = getPageModel(params, list, total);
            return ResultBody.ok(pageModel);
        } else {
            return ResultBody.ok(list);
        }
    }


    /**
     * 销售单分页列表
     *
     * @param dbName
     * @param params
     * @param isPage
     * @return
     */
    @Override
    public ResultBody saleList(String dbName, Map params, Boolean isPage) {
        initParams(params);
        params.put("isPage", isPage);
        List<EntityMap> list = hyErpSearchMapper.saleList(dbName, params);
        if (isPage) {
            int total = hyErpSearchMapper.countSaleList(dbName, params);
            EntityMap pageModel = getPageModel(params, list, total);
            return ResultBody.ok(pageModel);
        } else {
            return ResultBody.ok(list);
        }
    }


    /**
     * 销售单明细分页列表
     *
     * @param dbName
     * @param params
     * @param isPage
     * @return
     */
    @Override
    public ResultBody saleDetailList(String dbName, Map params, Boolean isPage) {
        initParams(params);
        params.put("isPage", isPage);
        List<String> drugsIds = getDrugsIds(dbName);
        params.put("drugsIds", drugsIds);
        List<String> storeCodes = getStoreCodes(dbName);
        params.put("storeCodes", storeCodes);
        List<String> drugTypes = getDrugType(dbName);
        params.put("drugTypes", drugTypes);
        List<EntityMap> list = hyErpSearchMapper.saleDetailList(dbName, params);
        if (isPage) {
            int total = hyErpSearchMapper.countSaleDetailList(dbName, params);
            EntityMap pageModel = getPageModel(params, list, total);
            return ResultBody.ok(pageModel);
        } else {
            return ResultBody.ok(list);
        }
    }


    /**
     * 明细账分页列表
     *
     * @param dbName
     * @param params
     * @param isPage
     * @return
     */
    @Override
    public ResultBody mxzList(String dbName, Map params, Boolean isPage) {
        initParams(params);
        params.put("isPage", isPage);
        List<String> drugsIds = getDrugsIds(dbName);
        params.put("drugsIds", drugsIds);
        List<String> storeCodes = getStoreCodes(dbName);
        params.put("storeCodes", storeCodes);
        List<String> drugTypes = getDrugType(dbName);
        params.put("drugTypes", drugTypes);
        List<EntityMap> list = hyErpSearchMapper.mxzList(dbName, params);
        if (isPage) {
            int total = hyErpSearchMapper.countMxzList(dbName, params);
            EntityMap pageModel = getPageModel(params, list, total);
            return ResultBody.ok(pageModel);
        } else {
            return ResultBody.ok(list);
        }
    }

    @Override
    public List<EntityMap> selectHySaleManList(String dbName, Map params) {
        return hyErpSearchMapper.selectHySaleManList(dbName, params);
    }

    @Override
    public List<EntityMap> selectHyDeptList(String dbName, Map params) {
        return hyErpSearchMapper.selectHyDeptList(dbName, params);
    }

    @Override
    public List<EntityMap> selectDrugList(String dbName, Map params) {
        return hyErpSearchMapper.selectDrugList(dbName, params);
    }

    @Override
    public List<EntityMap> selectHyCustomList(String dbName, Map params) {
        return hyErpSearchMapper.selectHyCustomList(dbName, params);
    }

    @Override
    public List<EntityMap> selectSaleHistoryList(String dbName, Map params) {
        return hyErpSearchMapper.selectSaleHistoryList(dbName, params);
    }

    @Override
    public List<EntityMap> selectStockList(String dbName, Map params) {
        return hyErpSearchMapper.selectStockList(dbName, params);
    }

    @Override
    public String selectHyMaxSaleId(String dbName) {
        return hyErpSearchMapper.selectHyMaxSaleId(dbName);
    }

    @Override
    public String selectHySaleManByBusinessNo(String dbName, String saleManBusinessNo) {
        return hyErpSearchMapper.selectHySaleManByBusinessNo(dbName, saleManBusinessNo);
    }

    /**
     * 获取管控商品编码
     *
     * @param dbName
     * @return
     */
    private List<String> getDrugsIds(String dbName) {
        List<String> drugsIds = new ArrayList<>();
        Long userId = OpenHelper.getUserId();
        String tenantId = OpenHelper.getTenantId();
        List<Long> roleIds = baseRoleService.getRoleIds(userId, tenantId);
        List<String> drugsNames = dataScopeService.getDataScopeNames(userId, roleIds, "drug");
        if (FlymeUtils.isNotEmpty(drugsNames)) {
            drugsIds = hyErpSearchMapper.selectDrugIds(dbName, drugsNames);
        }
        return drugsIds;
    }

    /**
     * 获取管控仓库编码
     *
     * @param dbName
     * @return
     */
    private List<String> getStoreCodes(String dbName) {
        List<String> storeCodes = new ArrayList<>();
        Long userId = OpenHelper.getUserId();
        String tenantId = OpenHelper.getTenantId();
        List<Long> roleIds = baseRoleService.getRoleIds(userId, tenantId);
        List<String> storeNames = dataScopeService.getDataScopeNames(userId, roleIds, "store");
        if (FlymeUtils.isNotEmpty(storeNames)) {
            storeCodes = hyErpSearchMapper.selectStoreCodes(dbName, storeNames);
        }
        return storeCodes;
    }

    /**
     * 获取管控仓库编码
     *
     * @param dbName
     * @return
     */
    private List<String> getDrugType(String dbName) {
        Long userId = OpenHelper.getUserId();
        String tenantId = OpenHelper.getTenantId();
        List<Long> roleIds = baseRoleService.getRoleIds(userId, tenantId);
        List<String> storeCodes = dataScopeService.getDataScopeCodes(userId, roleIds, "drugType");
        if (FlymeUtils.isEmpty(storeCodes)) {
            return new ArrayList<>();
        }
        return storeCodes;
    }
}
